using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace LDNS.Interfaces
{
    /// <summary>
    /// Sockets管理接口
    /// 提供Socket连接的管理功能
    /// 作者：huobing
    /// </summary>
    public interface ISocketManager : IDisposable
    {
        /// <summary>
        /// 获取管理器是否正在运行
        /// </summary>
        bool IsRunning { get; }
        
        /// <summary>
        /// 获取当前连接的客户端数量
        /// </summary>
        int ClientCount { get; }
        
        /// <summary>
        /// 获取监听端口
        /// </summary>
        int Port { get; }
        
        /// <summary>
        /// 获取所有连接的客户端信息
        /// </summary>
        IReadOnlyList<Models.SocketClientInfo> ConnectedClients { get; }
        
        /// <summary>
        /// 启动Socket服务器
        /// </summary>
        /// <param name="port">监听端口</param>
        /// <returns>是否启动成功</returns>
        Task<bool> StartAsync(int port);
        
        /// <summary>
        /// 停止Socket服务器
        /// </summary>
        void Stop();
        
        /// <summary>
        /// 停止Socket服务器（异步版本）
        /// </summary>
        Task StopAsync();
        
        /// <summary>
        /// 发送消息到指定客户端
        /// </summary>
        /// <param name="clientId">客户端ID</param>
        /// <param name="message">消息内容</param>
        /// <returns>是否发送成功</returns>
        Task<bool> SendMessageAsync(string clientId, string message);
        
        /// <summary>
        /// 广播消息到所有客户端
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <returns>发送成功的客户端数量</returns>
        Task<int> BroadcastMessageAsync(string message);
        
        /// <summary>
        /// 断开指定客户端连接
        /// </summary>
        /// <param name="clientId">客户端ID</param>
        /// <returns>是否断开成功</returns>
        bool DisconnectClient(string clientId);
        
        /// <summary>
        /// 获取客户端连接统计信息
        /// </summary>
        /// <returns>连接统计信息</returns>
        SocketConnectionStatistics GetConnectionStatistics();
        
        /// <summary>
        /// 获取服务器状态信息
        /// </summary>
        /// <returns>服务器状态信息</returns>
        SocketServerStatus GetServerStatus();
    }
    

    
    /// <summary>
    /// Socket连接统计信息
    /// 作者：huobing
    /// </summary>
    public class SocketConnectionStatistics
    {
        /// <summary>
        /// 总连接数
        /// </summary>
        public int TotalConnections { get; set; }
        
        /// <summary>
        /// 当前连接数
        /// </summary>
        public int CurrentConnections { get; set; }
        
        /// <summary>
        /// 总接收消息数
        /// </summary>
        public int TotalMessagesReceived { get; set; }
        
        /// <summary>
        /// 总发送消息数
        /// </summary>
        public int TotalMessagesSent { get; set; }
        
        /// <summary>
        /// 服务器启动时间
        /// </summary>
        public DateTime ServerStartTime { get; set; }
        
        /// <summary>
        /// 服务器运行时长
        /// </summary>
        public TimeSpan ServerUptime => DateTime.Now - ServerStartTime;
    }
    
    /// <summary>
    /// Socket服务器状态信息
    /// 作者：huobing
    /// </summary>
    public class SocketServerStatus
    {
        /// <summary>
        /// 服务器是否正在运行
        /// </summary>
        public bool IsRunning { get; set; }
        
        /// <summary>
        /// 监听端口
        /// </summary>
        public int Port { get; set; }
        
        /// <summary>
        /// 当前连接数
        /// </summary>
        public int CurrentConnections { get; set; }
        
        /// <summary>
        /// 最大连接数限制
        /// </summary>
        public int MaxConnections { get; set; }
        
        /// <summary>
        /// 服务器启动时间
        /// </summary>
        public DateTime? StartTime { get; set; }
        
        /// <summary>
        /// 服务器运行时长
        /// </summary>
        public TimeSpan? Uptime => StartTime.HasValue ? DateTime.Now - StartTime.Value : TimeSpan.Zero;
        
        /// <summary>
        /// 服务器性能指标
        /// </summary>
        public SocketPerformanceMetrics PerformanceMetrics { get; set; } = new SocketPerformanceMetrics();
    }
    
    /// <summary>
    /// Socket性能指标
    /// 作者：huobing
    /// </summary>
    public class SocketPerformanceMetrics
    {
        /// <summary>
        /// 平均消息处理时间（毫秒）
        /// </summary>
        public double AverageMessageProcessingTimeMs { get; set; }
        
        /// <summary>
        /// 消息处理成功率
        /// </summary>
        public double MessageProcessingSuccessRate { get; set; }
        
        /// <summary>
        /// 连接成功率
        /// </summary>
        public double ConnectionSuccessRate { get; set; }
        
        /// <summary>
        /// 内存使用量（MB）
        /// </summary>
        public double MemoryUsageMB { get; set; }
    }
    

}